<?php
class ControllerSurveyReport extends Controller {
	private $error = array();

  	public function index() {
		$this->load->language('survey/report');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('survey/report');

    	$this->getList();
  	}
	
  	public function insert() {
		$this->load->language('survey/report');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('survey/report');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
      	  	$this->model_survey_report->addAnswer($this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');
		  
			$url = '';
			
			if (isset($this->request->get['filter_answer_id'])) {
				$url .= '&filter_answer_id=' . $this->request->get['filter_answer_id'];
			}
			
			if (isset($this->request->get['filter_customer'])) {
				$url .= '&filter_customer=' . $this->request->get['filter_customer'];
			}
												
			if (isset($this->request->get['filter_answer_status_id'])) {
				$url .= '&filter_answer_status_id=' . $this->request->get['filter_answer_status_id'];
			}
			
			if (isset($this->request->get['filter_total'])) {
				$url .= '&filter_total=' . $this->request->get['filter_total'];
			}
						
			if (isset($this->request->get['filter_date_added'])) {
				$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
			}
			
			if (isset($this->request->get['filter_date_modified'])) {
				$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
			}
													
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['answer'])) {
				$url .= '&answer=' . $this->request->get['answer'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('sale/return', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}
		
    	$this->getForm();
  	}
	
  	public function update() {
		$this->load->language('survey/report');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('survey/report');
    	
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_survey_report->editAnswer($this->request->get['answer_id'], $this->request->post);
	  		
			$this->session->data['success'] = $this->language->get('text_success');
	  
			$url = '';

			if (isset($this->request->get['filter_answer_id'])) {
				$url .= '&filter_answer_id=' . $this->request->get['filter_answer_id'];
			}
			
			if (isset($this->request->get['filter_customer'])) {
				$url .= '&filter_customer=' . $this->request->get['filter_customer'];
			}
												
			if (isset($this->request->get['filter_answer_status_id'])) {
				$url .= '&filter_answer_status_id=' . $this->request->get['filter_answer_status_id'];
			}
			
			if (isset($this->request->get['filter_total'])) {
				$url .= '&filter_total=' . $this->request->get['filter_total'];
			}
						
			if (isset($this->request->get['filter_date_added'])) {
				$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
			}
			
			if (isset($this->request->get['filter_date_modified'])) {
				$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
			}
													
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['answer'])) {
				$url .= '&answer=' . $this->request->get['answer'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('sale/return', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}
		
    	$this->getForm();
  	}
	
  	public function delete() {
		$this->load->language('survey/report');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('survey/report');

    	if (isset($this->request->post['selected']) && ($this->validateDelete())) {
			foreach ($this->request->post['selected'] as $answer_id) {
				$this->model_survey_report->deleteAnswer($answer_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['filter_answer_id'])) {
				$url .= '&filter_answer_id=' . $this->request->get['filter_answer_id'];
			}
			
			if (isset($this->request->get['filter_customer'])) {
				$url .= '&filter_customer=' . $this->request->get['filter_customer'];
			}
												
			if (isset($this->request->get['filter_answer_status_id'])) {
				$url .= '&filter_answer_status_id=' . $this->request->get['filter_answer_status_id'];
			}
			
			if (isset($this->request->get['filter_total'])) {
				$url .= '&filter_total=' . $this->request->get['filter_total'];
			}
						
			if (isset($this->request->get['filter_date_added'])) {
				$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
			}
			
			if (isset($this->request->get['filter_date_modified'])) {
				$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
			}
													
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['answer'])) {
				$url .= '&answer=' . $this->request->get['answer'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('survey/report', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    	}

    	$this->getList();
  	}

  	private function getList() {
		$this->load->language('survey/report');

		$this->load->model('survey/report');
		
		if(isset($this->request->get['survey_id']))
			$survey_id = $this->request->get['survey_id'];
		else
			$survey_id = $this->session->data['config_survey_id'];
		
		if(empty($survey_id))
			die("survey id missing.");
		
		$survey_jnfo = $this->model_survey_report->getSurvey($survey_id);
		
		$this->load->model('survey/question');
		$this->load->model('survey/answer');
				
		$this->document->setTitle($survey_jnfo['title']);
		
		if (isset($this->request->get['filter'])) {
			$this->data['hide_filters'] =  0;
			$filter = $this->request->get['filter'];
		} else {
			$this->data['hide_filters'] =  1;
			$filter = '1';
		}
		
		if(isset($this->request->post['filter_module']))
   			$filter_module = $this->request->post['filter_module'];
   		else
   			$filter_module = array();
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'i.sort_order';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$url = '';
		
		if (isset($this->request->get['filter'])) {
			$url .= '&filter=' . $this->request->get['filter'];
		} else if($filter) {
			$url .= '&filter='.$filter;
		}
			
		if (isset($this->request->get['survey_id'])) {
			$url .= '&survey_id=' . $this->request->get['survey_id'];
		}			
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('survey/report', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $survey_jnfo['title'],
			'href'      => 'javascript:void(0)',
      		'separator' => ' :: '
   		);
   		
   		#------------------ Filter Data Start ---------------------------#
   		$this->data['default_critaria'] = 'eq';
   		$this->data['filter_module'] = $filter_module;
   		$this->data['filter'] = $filter;
   		$this->data['survey_id'] = $survey_id;
   		$this->data['token'] = $this->session->data['token'];
   		$this->data['filter_action'] = $this->url->link('survey/report', 'token=' . $this->session->data['token'] . $url, 'SSL');					
   		$this->data['reset_link'] = $this->url->link('survey/report', 'token=' . $this->session->data['token'] . '&survey_id=' . $this->request->get['survey_id'], 'SSL');
   		
   		$data = array(
			'filter_survey_id' => $survey_id,
			'filter_module' => $filter_module,
		);
		
//		print_r($filter_module);
   		
   		ob_start();
   		$filtered_emails = array();
   		$this->model_survey_report->getFilteredParticipentEmail($data, $filtered_emails);
   		ob_clean();
   		
   		if(count($filter_module) <= 0) {
   			$filtered_emails = null;
   		}
   		
//   		var_dump($filtered_emails);
//   		die;
		#------------------ Filter Data End -----------------------------#
   		
		
		//Custome conditions for overriding the report  
		$show_custom = 0;
		if(isset($_GET['show_custom'])) {
			$show_custom = 1;
		}
		
   		if($show_custom) {
   			$filtered_emails = array();
   			$filter_sql = " AND email not in (SELECT distinct email FROM " . DB_PREFIX . "answer where question_id in ('59','60','61')) ";
   			$sql = "SELECT email FROM " . DB_PREFIX . "answer WHERE survey_id='".$survey_id."' ".$filter_sql;
//   			echo $sql;
   			$query = $this->db->query($sql);				
			$results = $query->rows;
			
			if(!empty($results)) {
				$filtered_emails = array();
				foreach($results as $each_email) {
					$filtered_emails[] = $each_email['email'];	
				}				
			}
   		}
							
		$this->data['participents'] = $this->url->link('survey/report/participents', 'token=' . $this->session->data['token'] . $url, 'SSL');			
		$this->data['back'] = $this->url->link('common/home', 'token=' . $this->session->data['token'] . $url, 'SSL');			

		$this->data['surveys'] = array();

		$data = array(
			'filter_survey_id' => $survey_id,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')			
		);
		
		$survey_total = $this->model_survey_question->getTotalQuestions($data);
	
		$results = $this->model_survey_question->getQuestions($data);
		
//		print_r($results);		
 
    	foreach ($results as $result) {
    		$answer = array();
    		$options = array();
    		$question_data = array();
    		
    		$question_info = $result;
      		$question_id = $question_info['question_id'];
      		
      		switch($question_info['qtype']) {
      		case 'text':
      		case 'textarea':
      		
      			$question_data = array(
      				'filter_survey_id' => $survey_id,
      				'filter_question_id' => $question_id,
      				'filter_check_answer' => 'not-empty',
      				'filtered_emails' => $filtered_emails,
	      		);
	      			
	      		$total_participant = $this->model_survey_report->getTotalParticipent($question_data);
	      		
	      		$check_answer = "not-empty";
	      		
	      		$answer[] = array(
					'text' => 'Have Some Text',
					'value' => $total_participant,
					'link' => $this->url->link('survey/report/participents', 'token=' . $this->session->data['token'] . '&survey_id=' . $survey_id . '&question_id=' . $question_id . '&filter_answer=' . $check_answer)				
				);
      		break;
      		case 'checkbox':
      		case 'radio':
	      		$question_info['lable'] = str_replace("\r","", $question_info['lable']);
	      		$all_lables = explode("\n", $question_info['lable']);
	      		if(!empty($question_info['value'])) {
	      			$question_info['value'] = str_replace("\r","", $question_info['value']);
	      			$all_values = explode("\n", $question_info['value']);
	      		} else {
	      			$all_values = $all_lables;
	      		}      			
	      		
	      		if(!empty($check_answer['answer']))
	      			$default_value = $check_answer['answer'];
	      		else
	      			$default_value = $question_info['default'];
	      		
	      		foreach($all_lables as $key => $each_value) {
	      			      			
	      			$default = 0;
	      			if($default_value == $all_values[$key])
	      				$default = 1;
	      				
	      		    $options[] = array(
		      			'lable' => $all_lables[$key],
		      			'value' => $all_values[$key],
		      			'default' => $default
		      		);	
	      		}
	      		
	      		foreach($options as $each_option) {
	      			
	      			$question_data = array(
	      				'filter_survey_id' => $survey_id,
	      				'filter_question_id' => $question_id,
	      				'filter_answer' => $each_option['value'],
      					'filtered_emails' => $filtered_emails,
	      			);
	      			
	      			$total_participant = $this->model_survey_report->getTotalParticipent($question_data);
	
					$answer[] = array(
						'text' => $each_option['lable'],
						'value' => $total_participant,
						'link' => $this->url->link('survey/report/participents', 'token=' . $this->session->data['token'] . '&survey_id=' . $survey_id . '&question_id=' . $question_id . '&filter_answer=' . urlencode($each_option['value']))
					);      			
	      		}
	      		
    			break;
    			case 'default' :
    			case 'lable' :
    			$answer = array();
    			break;
    		}
				
			$this->data['questions'][] = array(
				'question_id' => $result['question_id'],
				'title'          => nl2br($result['title']),
				'sort_order'     => $result['sort_order'],
				'answer'     => $answer,
				'selected'       => isset($this->request->post['selected']) && in_array($result['question_id'], $this->request->post['selected'])
			);
		}	
	
		$this->data['heading_title'] = $survey_jnfo['title'];

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_title'] = $this->language->get('column_title');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_answer_stats'] = $this->language->get('column_answer_stats');		
		$this->data['column_id'] = $this->language->get('column_id');		
		
		$this->data['button_participents'] = $this->language->get('button_all_participents');
		$this->data['button_back'] = $this->language->get('button_back');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_id'] = 'javascript:void(0)';
		$this->data['sort_title'] = 'javascript:void(0)';
		$this->data['sort_stats'] = 'javascript:void(0)';
		$this->data['sort_sort_order'] = 'javascript:void(0)';
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $survey_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('survey/report', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();		
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'survey/report_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
  	}

  	public function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');
		 
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_select'] = $this->language->get('text_select');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_wait'] = $this->language->get('text_wait');
		
		$this->data['entry_store'] = $this->language->get('entry_store');
		$this->data['entry_customer'] = $this->language->get('entry_customer');
		$this->data['entry_firstname'] = $this->language->get('entry_firstname');
		$this->data['entry_lastname'] = $this->language->get('entry_lastname');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_telephone'] = $this->language->get('entry_telephone');
		$this->data['entry_fax'] = $this->language->get('entry_fax');
		$this->data['entry_address'] = $this->language->get('entry_address');
		$this->data['entry_company'] = $this->language->get('entry_company');
		$this->data['entry_address_1'] = $this->language->get('entry_address_1');
		$this->data['entry_address_2'] = $this->language->get('entry_address_2');
		$this->data['entry_city'] = $this->language->get('entry_city');
		$this->data['entry_postcode'] = $this->language->get('entry_postcode');
		$this->data['entry_zone'] = $this->language->get('entry_zone');
		$this->data['entry_zone_code'] = $this->language->get('entry_zone_code');
		$this->data['entry_country'] = $this->language->get('entry_country');		
		$this->data['entry_product'] = $this->language->get('entry_product');
		$this->data['entry_model'] = $this->language->get('entry_model');
		$this->data['entry_quantity'] = $this->language->get('entry_quantity');
		$this->data['entry_price'] = $this->language->get('entry_price');
		$this->data['entry_affiliate'] = $this->language->get('entry_affiliate');
		$this->data['entry_answer_status'] = $this->language->get('entry_answer_status');
		$this->data['entry_comment'] = $this->language->get('entry_comment');		
		$this->data['entry_shipping'] = $this->language->get('entry_shipping');
		$this->data['entry_payment'] = $this->language->get('entry_payment');
		$this->data['entry_total'] = $this->language->get('entry_total');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_product'] = $this->language->get('button_add_product');
		$this->data['button_remove'] = $this->language->get('button_remove');

		$this->data['tab_answer'] = $this->language->get('tab_answer');
		$this->data['tab_product'] = $this->language->get('tab_product');
		$this->data['tab_payment'] = $this->language->get('tab_payment');
		$this->data['tab_shipping'] = $this->language->get('tab_shipping');
		$this->data['tab_total'] = $this->language->get('tab_total');

		$this->data['token'] = $this->session->data['token'];

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
 		if (isset($this->error['firstname'])) {
			$this->data['error_firstname'] = $this->error['firstname'];
		} else {
			$this->data['error_firstname'] = '';
		}

 		if (isset($this->error['lastname'])) {
			$this->data['error_lastname'] = $this->error['lastname'];
		} else {
			$this->data['error_lastname'] = '';
		}
		
 		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}
		
 		if (isset($this->error['telephone'])) {
			$this->data['error_telephone'] = $this->error['telephone'];
		} else {
			$this->data['error_telephone'] = '';
		}

 		if (isset($this->error['shipping_firstname'])) {
			$this->data['error_shipping_firstname'] = $this->error['shipping_firstname'];
		} else {
			$this->data['error_shipping_firstname'] = '';
		}

 		if (isset($this->error['shipping_lastname'])) {
			$this->data['error_shipping_lastname'] = $this->error['shipping_lastname'];
		} else {
			$this->data['error_shipping_lastname'] = '';
		}
				
		if (isset($this->error['shipping_address_1'])) {
			$this->data['error_shipping_address_1'] = $this->error['shipping_address_1'];
		} else {
			$this->data['error_shipping_address_1'] = '';
		}
		
		if (isset($this->error['shipping_city'])) {
			$this->data['error_shipping_city'] = $this->error['shipping_city'];
		} else {
			$this->data['error_shipping_city'] = '';
		}
		
		if (isset($this->error['shipping_postcode'])) {
			$this->data['error_shipping_postcode'] = $this->error['shipping_postcode'];
		} else {
			$this->data['error_shipping_postcode'] = '';
		}
		
		if (isset($this->error['shipping_country'])) {
			$this->data['error_shipping_country'] = $this->error['shipping_country'];
		} else {
			$this->data['error_shipping_country'] = '';
		}
		
		if (isset($this->error['shipping_zone'])) {
			$this->data['error_shipping_zone'] = $this->error['shipping_zone'];
		} else {
			$this->data['error_shipping_zone'] = '';
		}

 		if (isset($this->error['payment_firstname'])) {
			$this->data['error_payment_firstname'] = $this->error['payment_firstname'];
		} else {
			$this->data['error_payment_firstname'] = '';
		}

 		if (isset($this->error['payment_lastname'])) {
			$this->data['error_payment_lastname'] = $this->error['payment_lastname'];
		} else {
			$this->data['error_payment_lastname'] = '';
		}
				
		if (isset($this->error['payment_address_1'])) {
			$this->data['error_payment_address_1'] = $this->error['payment_address_1'];
		} else {
			$this->data['error_payment_address_1'] = '';
		}
		
		if (isset($this->error['payment_city'])) {
			$this->data['error_payment_city'] = $this->error['payment_city'];
		} else {
			$this->data['error_payment_city'] = '';
		}
		
		if (isset($this->error['payment_postcode'])) {
			$this->data['error_payment_postcode'] = $this->error['payment_postcode'];
		} else {
			$this->data['error_payment_postcode'] = '';
		}
		
		if (isset($this->error['payment_country'])) {
			$this->data['error_payment_country'] = $this->error['payment_country'];
		} else {
			$this->data['error_payment_country'] = '';
		}
		
		if (isset($this->error['payment_zone'])) {
			$this->data['error_payment_zone'] = $this->error['payment_zone'];
		} else {
			$this->data['error_payment_zone'] = '';
		}
				
		$url = '';

		if (isset($this->request->get['filter_answer_id'])) {
			$url .= '&filter_answer_id=' . $this->request->get['filter_answer_id'];
		}
		
		if (isset($this->request->get['filter_customer'])) {
			$url .= '&filter_customer=' . $this->request->get['filter_customer'];
		}
											
		if (isset($this->request->get['filter_answer_status_id'])) {
			$url .= '&filter_answer_status_id=' . $this->request->get['filter_answer_status_id'];
		}
		
		if (isset($this->request->get['filter_total'])) {
			$url .= '&filter_total=' . $this->request->get['filter_total'];
		}
					
		if (isset($this->request->get['filter_date_added'])) {
			$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
		}
		
		if (isset($this->request->get['filter_date_modified'])) {
			$url .= '&filter_date_modified=' . $this->request->get['filter_date_modified'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['answer'])) {
			$url .= '&answer=' . $this->request->get['answer'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('survey/report', 'token=' . $this->session->data['token'], 'SSL'),				
			'separator' => ' :: '
		);

		if (!isset($this->request->get['answer_id'])) {
			$this->data['action'] = $this->url->link('survey/report/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('survey/report/update', 'token=' . $this->session->data['token'] . '&answer_id=' . $this->request->get['answer_id'] . $url, 'SSL');
		}
		
		$this->data['cancel'] = $this->url->link('survey/report', 'token=' . $this->session->data['token'] . $url, 'SSL');

    	if (isset($this->request->get['answer_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$answer_info = $this->model_survey_report->getAnswer($this->request->get['answer_id']);
    	}
			
    	if (isset($this->request->post['store_id'])) {
      		$this->data['store_id'] = $this->request->post['store_id'];
    	} elseif (isset($answer_info)) { 
			$this->data['store_id'] = $answer_info['store_id'];
		} else {
      		$this->data['store_id'] = '';
    	}
		
		$this->load->model('setting/store');
		
		$this->data['stores'] = $this->model_setting_store->getStores();
		
		$this->data['store_url'] = HTTP_CATALOG;
				
		if (isset($this->request->post['customer_id'])) {
			$this->data['customer_id'] = $this->request->post['customer_id'];
		} elseif (isset($answer_info)) {
			$this->data['customer_id'] = $answer_info['customer_id'];
		} else {
			$this->data['customer_id'] = '';
		}
				
		if (isset($this->request->post['customer'])) {
			$this->data['customer'] = $this->request->post['customer'];
		} elseif (isset($answer_info)) {
			$this->data['customer'] = $answer_info['customer'];
		} else {
			$this->data['customer'] = '';
		}
				
    	if (isset($this->request->post['firstname'])) {
      		$this->data['firstname'] = $this->request->post['firstname'];
		} elseif (isset($answer_info)) { 
			$this->data['firstname'] = $answer_info['firstname'];
		} else {
      		$this->data['firstname'] = '';
    	}

    	if (isset($this->request->post['lastname'])) {
      		$this->data['lastname'] = $this->request->post['lastname'];
    	} elseif (isset($answer_info)) { 
			$this->data['lastname'] = $answer_info['lastname'];
		} else {
      		$this->data['lastname'] = '';
    	}

    	if (isset($this->request->post['email'])) {
      		$this->data['email'] = $this->request->post['email'];
    	} elseif (isset($answer_info)) { 
			$this->data['email'] = $answer_info['email'];
		} else {
      		$this->data['email'] = '';
    	}
				
    	if (isset($this->request->post['telephone'])) {
      		$this->data['telephone'] = $this->request->post['telephone'];
    	} elseif (isset($answer_info)) { 
			$this->data['telephone'] = $answer_info['telephone'];
		} else {
      		$this->data['telephone'] = '';
    	}
		
    	if (isset($this->request->post['fax'])) {
      		$this->data['fax'] = $this->request->post['fax'];
    	} elseif (isset($answer_info)) { 
			$this->data['fax'] = $answer_info['fax'];
		} else {
      		$this->data['fax'] = '';
    	}	

		$this->load->model('sale/customer');

		if (isset($this->request->post['customer_id'])) {
			$this->data['addresses'] = $this->model_sale_customer->getAddresses($this->request->post['customer_id']);
		} elseif (isset($answer_info)) {
			$this->data['addresses'] = $this->model_sale_customer->getAddresses($answer_info['customer_id']);
		} else {
			$this->data['addresses'] = array();
		}
			
    	if (isset($this->request->post['shipping_firstname'])) {
      		$this->data['shipping_firstname'] = $this->request->post['shipping_firstname'];
		} elseif (isset($answer_info)) { 
			$this->data['shipping_firstname'] = $answer_info['shipping_firstname'];
		} else {
      		$this->data['shipping_firstname'] = '';
    	}

    	if (isset($this->request->post['shipping_lastname'])) {
      		$this->data['shipping_lastname'] = $this->request->post['shipping_lastname'];
    	} elseif (isset($answer_info)) { 
			$this->data['shipping_lastname'] = $answer_info['shipping_lastname'];
		} else {
      		$this->data['shipping_lastname'] = '';
    	}

    	if (isset($this->request->post['shipping_company'])) {
      		$this->data['shipping_company'] = $this->request->post['shipping_company'];
    	} elseif (isset($answer_info)) { 
			$this->data['shipping_company'] = $answer_info['shipping_company'];
		} else {
      		$this->data['shipping_company'] = '';
    	}

    	if (isset($this->request->post['shipping_address_1'])) {
      		$this->data['shipping_address_1'] = $this->request->post['shipping_address_1'];
    	} elseif (isset($answer_info)) { 
			$this->data['shipping_address_1'] = $answer_info['shipping_address_1'];
		} else {
      		$this->data['shipping_address_1'] = '';
    	}

    	if (isset($this->request->post['shipping_address_2'])) {
      		$this->data['shipping_address_2'] = $this->request->post['shipping_address_2'];
    	} elseif (isset($answer_info)) { 
			$this->data['shipping_address_2'] = $answer_info['shipping_address_2'];
		} else {
      		$this->data['shipping_address_2'] = '';
    	}
		
    	if (isset($this->request->post['shipping_city'])) {
      		$this->data['shipping_city'] = $this->request->post['shipping_city'];
    	} elseif (isset($answer_info)) { 
			$this->data['shipping_city'] = $answer_info['shipping_city'];
		} else {
      		$this->data['shipping_city'] = '';
    	}
		
    	if (isset($this->request->post['shipping_postcode'])) {
      		$this->data['shipping_postcode'] = $this->request->post['shipping_postcode'];
    	} elseif (isset($answer_info)) { 
			$this->data['shipping_postcode'] = $answer_info['shipping_postcode'];
		} else {
      		$this->data['shipping_postcode'] = '';
    	}
				
    	if (isset($this->request->post['shipping_country_id'])) {
      		$this->data['shipping_country_id'] = $this->request->post['shipping_country_id'];
    	} elseif (isset($answer_info)) { 
			$this->data['shipping_country_id'] = $answer_info['shipping_country_id'];
		} else {
      		$this->data['shipping_country_id'] = '';
    	}		
	    
		if (isset($this->request->post['shipping_zone_id'])) {
      		$this->data['shipping_zone_id'] = $this->request->post['shipping_zone_id'];
    	} elseif (isset($answer_info)) { 
			$this->data['shipping_zone_id'] = $answer_info['shipping_zone_id'];
		} else {
      		$this->data['shipping_zone_id'] = '';
    	}	
		
    	if (isset($this->request->post['shipping_method'])) {
      		$this->data['shipping_method'] = $this->request->post['shipping_method'];
    	} elseif (isset($answer_info)) { 
			$this->data['shipping_method'] = $answer_info['shipping_method'];
		} else {
      		$this->data['shipping_method'] = '';
    	}	
				
    	if (isset($this->request->post['payment_firstname'])) {
      		$this->data['payment_firstname'] = $this->request->post['payment_firstname'];
		} elseif (isset($answer_info)) { 
			$this->data['payment_firstname'] = $answer_info['payment_firstname'];
		} else {
      		$this->data['payment_firstname'] = '';
    	}

    	if (isset($this->request->post['payment_lastname'])) {
      		$this->data['payment_lastname'] = $this->request->post['payment_lastname'];
    	} elseif (isset($answer_info)) { 
			$this->data['payment_lastname'] = $answer_info['payment_lastname'];
		} else {
      		$this->data['payment_lastname'] = '';
    	}

    	if (isset($this->request->post['payment_company'])) {
      		$this->data['payment_company'] = $this->request->post['payment_company'];
    	} elseif (isset($answer_info)) { 
			$this->data['payment_company'] = $answer_info['payment_company'];
		} else {
      		$this->data['payment_company'] = '';
    	}

    	if (isset($this->request->post['payment_address_1'])) {
      		$this->data['payment_address_1'] = $this->request->post['payment_address_1'];
    	} elseif (isset($answer_info)) { 
			$this->data['payment_address_1'] = $answer_info['payment_address_1'];
		} else {
      		$this->data['payment_address_1'] = '';
    	}

    	if (isset($this->request->post['payment_address_2'])) {
      		$this->data['payment_address_2'] = $this->request->post['payment_address_2'];
    	} elseif (isset($answer_info)) { 
			$this->data['payment_address_2'] = $answer_info['payment_address_2'];
		} else {
      		$this->data['payment_address_2'] = '';
    	}
		
    	if (isset($this->request->post['payment_city'])) {
      		$this->data['payment_city'] = $this->request->post['payment_city'];
    	} elseif (isset($answer_info)) { 
			$this->data['payment_city'] = $answer_info['payment_city'];
		} else {
      		$this->data['payment_city'] = '';
    	}

    	if (isset($this->request->post['payment_postcode'])) {
      		$this->data['payment_postcode'] = $this->request->post['payment_postcode'];
    	} elseif (isset($answer_info)) { 
			$this->data['payment_postcode'] = $answer_info['payment_postcode'];
		} else {
      		$this->data['payment_postcode'] = '';
    	}
				
    	if (isset($this->request->post['payment_country_id'])) {
      		$this->data['payment_country_id'] = $this->request->post['payment_country_id'];
    	} elseif (isset($answer_info)) { 
			$this->data['payment_country_id'] = $answer_info['payment_country_id'];
		} else {
      		$this->data['payment_country_id'] = '';
    	}		
	    
		if (isset($this->request->post['payment_zone_id'])) {
      		$this->data['payment_zone_id'] = $this->request->post['payment_zone_id'];
    	} elseif (isset($answer_info)) { 
			$this->data['payment_zone_id'] = $answer_info['payment_zone_id'];
		} else {
      		$this->data['payment_zone_id'] = '';
    	}
		
		$this->load->model('localisation/country');
		
		$this->data['countries'] = $this->model_localisation_country->getCountries();															
		
    	if (isset($this->request->post['payment_method'])) {
      		$this->data['payment_method'] = $this->request->post['payment_method'];
    	} elseif (isset($answer_info)) { 
			$this->data['payment_method'] = $answer_info['payment_method'];
		} else {
      		$this->data['payment_method'] = '';
    	}
		
		if (isset($this->request->post['affiliate_id'])) {
      		$this->data['affiliate_id'] = $this->request->post['affiliate_id'];
    	} elseif (isset($answer_info)) { 
			$this->data['affiliate_id'] = $answer_info['affiliate_id'];
		} else {
      		$this->data['affiliate_id'] = '';
    	}
		
		if (isset($this->request->post['affiliate'])) {
      		$this->data['affiliate'] = $this->request->post['affiliate'];
    	} elseif (isset($answer_info)) { 
			$this->data['affiliate'] = $answer_info['affiliate_firstname'] . '' . $answer_info['affiliate_lastname'];
		} else {
      		$this->data['affiliate'] = '';
    	}
				
		if (isset($this->request->post['answer_status_id'])) {
      		$this->data['answer_status_id'] = $this->request->post['answer_status_id'];
    	} elseif (isset($answer_info)) { 
			$this->data['answer_status_id'] = $answer_info['answer_status_id'];
		} else {
      		$this->data['answer_status_id'] = '';
    	}
			
		$this->load->model('localisation/answer_status');
		
		$this->data['answer_statuses'] = $this->model_localisation_answer_status->getAnswerStatuses();	
			
    	if (isset($this->request->post['comment'])) {
      		$this->data['comment'] = $this->request->post['comment'];
    	} elseif (isset($answer_info)) { 
			$this->data['comment'] = $answer_info['comment'];
		} else {
      		$this->data['comment'] = '';
    	}	
		
		if (isset($this->request->post['answer_product'])) {
			$answer_products = $this->request->post['answer_product'];
		} elseif (isset($answer_info)) {
			$answer_products = $this->model_survey_report->getAnswerProducts($this->request->get['answer_id']);			
		} else {
			$answer_products = array();
		}
		
		$this->load->model('catalog/product');
		
		$this->data['answer_products'] = array();		
		
		foreach ($answer_products as $answer_product) {
			$product_info = $this->model_catalog_product->getProduct($answer_product['product_id']);
			
			if ($product_info) {
				$option_data = array();
				
				//$this->data['answer_products'][] = $this->model_survey_report->getAnswerOptions($this->request->get['answer_id'], $product_option['option_id']);	
				
				$product_options = $this->model_catalog_product->getProductOptions($answer_product['product_id']);	
				
				foreach ($product_options as $product_option) {
					if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox') {
						$option_value_data = array();
						
						foreach ($product_option['product_option_value'] as $product_option_value) {
							$option_value_data[] = array(
								'product_option_value_id' => $product_option_value['product_option_value_id'],
								'option_value_id'         => $product_option_value['option_value_id'],
								'name'                    => $product_option_value['name'],
								'price'                   => (float)$product_option_value['price'] ? $this->currency->format($product_option_value['price'], $this->config->get('config_currency')) : false,
								'price_prefix'            => $product_option_value['price_prefix']
							);	
						}
						
						$option_data[] = array(
							'product_option_id' => $product_option['product_option_id'],
							'option_id'         => $product_option['option_id'],
							'name'              => $product_option['name'],
							'type'              => $product_option['type'],
							'option_value'      => $option_value_data,
							'required'          => $product_option['required']
						);	
					} else {
						$option_data[] = array(
							'product_option_id' => $product_option['product_option_id'],
							'option_id'         => $product_option['option_id'],
							'name'              => $product_option['name'],
							'type'              => $product_option['type'],
							'option_value'      => $product_option['option_value'],
							'required'          => $product_option['required']
						);				
					}
				}
				
				$this->data['answer_products'][] = array(
					'answer_product_id' => $answer_product['answer_product_id'],
					'answer_id'         => $answer_product['answer_id'],
					'product_id'       => $product_info['product_id'],
					'name'             => $product_info['name'],
					'model'            => $product_info['model'],
					'option'           => $option_data,
					'quantity'         => $answer_product['quantity'],
					'price'            => $answer_product['price'],
					'total'            => $answer_product['total'],
					'tax'              => $answer_product['tax']
				);
			}
		}
		   
		if (isset($this->request->post['answer_total'])) {
      		$this->data['answer_totals'] = $this->request->post['answer_total'];
    	} elseif (isset($answer_info)) { 
			$this->data['answer_totals'] = $this->model_survey_report->getAnswerTotals($this->request->get['answer_id']);
		} else {
      		$this->data['answer_totals'] = array();
    	}	
		
		$this->template = 'survey/report_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
		
		$this->response->setOutput($this->render());
  	}
	
  	private function validateForm() {
    	if (!$this->user->hasPermission('modify', 'survey/report')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}

    	if ((strlen(utf8_decode($this->request->post['firstname'])) < 1) || (strlen(utf8_decode($this->request->post['firstname'])) > 32)) {
      		$this->error['firstname'] = $this->language->get('error_firstname');
    	}

    	if ((strlen(utf8_decode($this->request->post['lastname'])) < 1) || (strlen(utf8_decode($this->request->post['lastname'])) > 32)) {
      		$this->error['lastname'] = $this->language->get('error_lastname');
    	}

    	if ((strlen(utf8_decode($this->request->post['email'])) > 96) || (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL))) {
      		$this->error['email'] = $this->language->get('error_email');
    	}
		
    	if ((strlen(utf8_decode($this->request->post['telephone'])) < 3) || (strlen(utf8_decode($this->request->post['telephone'])) > 32)) {
      		$this->error['telephone'] = $this->language->get('error_telephone');
    	}

    	if ((strlen(utf8_decode($this->request->post['shipping_firstname'])) < 1) || (strlen(utf8_decode($this->request->post['shipping_firstname'])) > 32)) {
      		$this->error['shipping_firstname'] = $this->language->get('error_firstname');
    	}

    	if ((strlen(utf8_decode($this->request->post['shipping_lastname'])) < 1) || (strlen(utf8_decode($this->request->post['shipping_lastname'])) > 32)) {
      		$this->error['shipping_lastname'] = $this->language->get('error_lastname');
    	}
		
    	if ((strlen(utf8_decode($this->request->post['shipping_address_1'])) < 3) || (strlen(utf8_decode($this->request->post['shipping_address_1'])) > 128)) {
      		$this->error['shipping_address_1'] = $this->language->get('error_address_1');
    	}

    	if ((strlen(utf8_decode($this->request->post['shipping_city'])) < 3) || (strlen(utf8_decode($this->request->post['shipping_city'])) > 128)) {
      		$this->error['shipping_city'] = $this->language->get('error_city');
    	}

		$this->load->model('localisation/country');
		
		$country_info = $this->model_localisation_country->getCountry($this->request->post['shipping_country_id']);
		
		if ($country_info && $country_info['postcode_required'] && (strlen(utf8_decode($this->request->post['shipping_postcode'])) < 2) || (strlen(utf8_decode($this->request->post['shipping_postcode'])) > 10)) {
			$this->error['shipping_postcode'] = $this->language->get('error_postcode');
		}

    	if ($this->request->post['shipping_country_id'] == '') {
      		$this->error['shipping_country'] = $this->language->get('error_country');
    	}
		
    	if ($this->request->post['shipping_zone_id'] == '') {
      		$this->error['shipping_zone'] = $this->language->get('error_zone');
    	}
		
    	if ((strlen(utf8_decode($this->request->post['payment_firstname'])) < 1) || (strlen(utf8_decode($this->request->post['payment_firstname'])) > 32)) {
      		$this->error['payment_firstname'] = $this->language->get('error_firstname');
    	}

    	if ((strlen(utf8_decode($this->request->post['payment_lastname'])) < 1) || (strlen(utf8_decode($this->request->post['payment_lastname'])) > 32)) {
      		$this->error['payment_lastname'] = $this->language->get('error_lastname');
    	}

    	if ((strlen(utf8_decode($this->request->post['payment_address_1'])) < 3) || (strlen(utf8_decode($this->request->post['payment_address_1'])) > 128)) {
      		$this->error['payment_address_1'] = $this->language->get('error_address_1');
    	}

    	if ((strlen(utf8_decode($this->request->post['payment_city'])) < 3) || (strlen(utf8_decode($this->request->post['payment_city'])) > 128)) {
      		$this->error['payment_city'] = $this->language->get('error_city');
    	}

		$country_info = $this->model_localisation_country->getCountry($this->request->post['payment_country_id']);
		
		if ($country_info && $country_info['postcode_required'] && (strlen(utf8_decode($this->request->post['payment_postcode'])) < 2) || (strlen(utf8_decode($this->request->post['payment_postcode'])) > 10)) {
			$this->error['payment_postcode'] = $this->language->get('error_postcode');
		}

    	if ($this->request->post['payment_country_id'] == '') {
      		$this->error['payment_country'] = $this->language->get('error_country');
    	}
		
    	if ($this->request->post['payment_zone_id'] == '') {
      		$this->error['payment_zone'] = $this->language->get('error_zone');
    	}		

		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
		
		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}
  	
  	public function options() {
  		$results = array();
		$output = '<option value="">' . $this->language->get('text_select') . '</option>'."\n";
		
		switch($this->request->get['name']) {
			case'answer':
				$this->load->model('survey/question');
				$question_id = $this->request->get['question_id'];
				$question = $this->model_survey_question->getQuestion($question_id);
				$question_description = $this->model_survey_question->getQuestionDescriptions($question_id);
				$question_description = current($question_description);
				
				$question_info = array_merge((array) $question, (array) $question_description);
				
//				print_r($question_info);
				
				$options = array();
					switch ($question_info['qtype']) {
		      			case 'radio' :
				      		$question_info['lable'] = str_replace("\r","", $question_info['lable']);
				      		$all_lables = explode("\n", $question_info['lable']);
				      		if(!empty($question_info['value'])) {
				      			$question_info['value'] = str_replace("\r","", $question_info['value']);
				      			$all_values = explode("\n", $question_info['value']);
				      		} else {
				      			$all_values = $all_lables;
				      		}      			
				      		
				      		$default_value = 0;
				      		if(isset($this->request->post['answer_'.$question_id])) {
//				      			echo "1";
				      			if(!empty($this->request->post['answer_'.$question_id]))
				      				$default_value = $this->request->post['answer_'.$question_id];
				      		} else if(!empty($check_answer['answer'])) {
//				      			echo "2";
				      			$default_value = $check_answer['answer'];
				      		} else if(!empty($question_info['default'])) {
//				      			echo "3";
				      			$default_value = $question_info['default'];
				      		}				      			
				      		
				      		
//				    		echo $default_value;
//				    		print_r($all_values);
				    		
				      		
				      		foreach($all_lables as $key => $each_value) {
				      			      			
				      			$default = 0;
				      			
				      			if(strcmp($default_value, $all_values[$key]) >= 0) {
				      				$default = 1;
				      			}				      				
				      			
				      		    $options[] = array(
					      			'lable' => $all_lables[$key],
					      			'value' => $all_values[$key],
					      			'default' => $default
					      		);	
				      		}      			
		      			break;
		      			case 'text' :
							$options = array(
				      			'lable' => $question_info['lable'],
				      			'value' => $question_info['value'],
				      			'default' => $question_info['default']
				      		);      					
		      			break;
		      			case 'textarea' :
							$options = array(
				      			'lable' => $question_info['lable'],
				      			'value' => $question_info['value'],
				      			'default' => $question_info['default']
				      		);      					
		      			break;
		      		}
		      		
//		      	print_r($options);				
				
				foreach($options as $each_row) {
					$results[] = array(
						'option_id' => $each_row['value'],
						'name' => $each_row['lable'],		
					);	
				}
			break;
			case'question':
				$this->load->model('survey/question');				
				$this->request->get['filter_survey_id'] = $this->request->get['survey_id'];
				$questions = $this->model_survey_question->getQuestions($this->request->get);
				
				foreach($questions as $each_row) {
					$results[] = array(
						'option_id' => $each_row['question_id'],
						'name' => "(".$each_row['qnum'].") ".$each_row['title'],		
					);	
				}	
			break;
			default:
				$this->load->model('survey/report');
				$results = $this->model_survey_report->getCritaria($this->request->get);	
			break;
		}
        
//        print_r($results);
        
      	foreach ($results as $result) {
        	$output .= '<option value="' . $result['option_id'] . '"';
	
	    	if (isset($this->request->get['select_id']) && ($this->request->get['select_id'] == $result['option_id'])) {
	      		$output .= ' selected="selected"';
	    	}
	
	    	$output .= '>' . $result['name'] . '</option>'."\n";
    	} 
		
		if (!$results) {
		  	$output .= '<option value="0">' . $this->language->get('text_none') . '</option>';
		}
	
		echo $output;
		exit;  	
  	}
  	
  	public function participents() {  		
		
		$this->load->language('survey/survey');

		$this->load->model('survey/survey');
		
		$survey_id = $this->request->get['survey_id'];
		
		$question_id = NULL;
		if(isset($this->request->get['filter_answer']))
			$question_id = $this->request->get['question_id'];
		
		$filter_answer = NULL;
		if(isset($this->request->get['filter_answer']))
			$filter_answer = $this->request->get['filter_answer'];
		
		$survey_jnfo = $this->model_survey_survey->getSurvey($survey_id);
		
		$this->load->model('survey/question');
		$this->load->model('survey/answer');
				
		$this->document->setTitle($survey_jnfo['title']);
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'i.sort_order';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$url = '';
		
		if (isset($this->request->get['filter_answer'])) {
			$url .= '&filter_answer=' . $this->request->get['filter_answer'];
		}
		
		if (isset($this->request->get['question_id'])) {
			$url .= '&question_id=' . $this->request->get['question_id'];
		}
		
		if (isset($this->request->get['survey_id'])) {
			$url .= '&survey_id=' . $this->request->get['survey_id'];
		}
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $survey_jnfo['title'],
			'href'      => $this->url->link('survey/survey/info', 'token=' . $this->session->data['token'] . '&survey_id='. $survey_id, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_participent'),
			'href'      => 'javascript:void(0)',
      		'separator' => ' :: '
   		);
							
		$this->data['back'] = $this->url->link('survey/report', 'token=' . $this->session->data['token'] . '&survey_id='. $survey_id, 'SSL');			

		$this->data['surveys'] = array();
		
		$question_data = array(
			'filter_survey_id' => $survey_id,
			'filter_question_id' => $question_id,
			'filter_answer' => (!is_null($filter_answer)) ? urldecode($filter_answer) : $filter_answer 
  		);

  		
//  		print_r($question_data);
 		
// 		$results = array_merge($total_guests, $total_customers);
 		$results =  $this->model_survey_survey->getParticipents($question_data);
 		$survey_total = count($results);
		
		$this->data['questions'] = array();
		
// 		print_r($results);

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'] - 1;
			if($page > 1) {
				$counter = ($page * $this->config->get('config_admin_limit'));
				$counter = $counter + 1;
			} else {
				$counter = 1;	
			}
				
		} else {
			$counter = 1;
		}
 		
    	foreach ($results as $result) {
    		$action = array();
			
			$action[] = array(
				'text' => 'View Answers',
				'href' => $this->url->link('survey/report/answers', 'token=' . $this->session->data['token'] . '&survey_id=' . $survey_id . '&user_id=' . $result['customer_id'] . $url, 'SSL')
			);		

				
			$this->data['questions'][] = array(
				'customer_id' => $result['customer_id'],
				'name'          => $result['name'],
				'type'          => ($result['guest_id']) ? 'Guest' : 'Customer',
				'sort_order'          => $counter,
				'action'          => $action,
				'selected'       => isset($this->request->post['selected']) && in_array($result['question_id'], $this->request->post['selected'])
			);
			
			$counter++;
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_participent');

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_action'] = $this->language->get('column_action');
		$this->data['column_type'] = $this->language->get('column_type');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_answer_stats'] = $this->language->get('column_answer_stats');		
		$this->data['column_id'] = $this->language->get('column_id');		
		
		$this->data['button_back'] = $this->language->get('button_back');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $survey_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
//		$this->data['pagination'] = $pagination->render();
		$this->data['pagination'] = false;

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'survey/participents.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());	
	
  	}
  	
  	public function answers() {
  		
		
		$this->load->language('survey/survey');

		$this->load->model('survey/survey');
		
		$survey_id = $this->request->get['survey_id'];
		
		$question_id = NULL;
		if(isset($this->request->get['question_id']))
			$question_id = $this->request->get['question_id'];
		
		
		$filter_answer = NULL;
		if(isset($this->request->get['question_id']))
			$filter_answer = $this->request->get['filter_answer'];		
		
		$user_info = array();
		$customer_id = NULL;
		if(isset($this->request->get['user_id'])) {
			$customer_id = $this->request->get['user_id'];
			
			$this->load->model('sale/customer');
			$user_info = $this->model_sale_customer->getCustomer($customer_id);	
		}			
		
		if(empty($customer_id))
			die("customer id missing.");

		
//		print_r($user_info);
		
		$survey_jnfo = $this->model_survey_survey->getSurvey($survey_id);
		$heading_title = $user_info['firstname'] . " " . $user_info['lastname'] . " (" . $user_info['email'] . ") ";
		
		$this->load->model('survey/question');
		$this->load->model('survey/answer');
				
		$this->document->setTitle($survey_jnfo['title']);
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'i.sort_order';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$url = '';
		
		if (isset($this->request->get['filter_answer'])) {
			$url .= '&filter_answer=' . $this->request->get['filter_answer'];
		}
		
		if (isset($this->request->get['question_id'])) {
			$url .= '&question_id=' . $this->request->get['question_id'];
		}
		
		if (isset($this->request->get['survey_id'])) {
			$url .= '&survey_id=' . $this->request->get['survey_id'];
		}
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $survey_jnfo['title'],
			'href'      => $this->url->link('survey/survey/info', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => 'Participents',
			'href'      => $this->url->link('survey/survey/participents', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
   		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $heading_title,
			'href'      => 'javascript:void(0)',
      		'separator' => ' :: '
   		);
							
		$this->data['back'] = $this->url->link('survey/report/participents', 'token=' . $this->session->data['token'] . $url, 'SSL');			

		$this->data['surveys'] = array();

		$data = array(
			'filter_survey_id' => $survey_id,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')			
		);
		
		$survey_total = $this->model_survey_question->getTotalQuestions($data);
	
		$results = $this->model_survey_question->getQuestions($data);
		
//		print_r($results);		
 
    	foreach ($results as $result) {
    		$answer = array();
    		
    		$question_id = $result['question_id'];
    		
    		$options = array();
    		$question_data = array();
    		
    		$question_info = $result;
      		$question_id = $question_info['question_id'];
      		
      		switch($question_info['qtype']) {
      		case 'text':
      		case 'textarea':
	      		$question_data = array(
	  				'filter_survey_id' => $survey_id,
	  				'filter_question_id' => $question_id,
	  				'filter_check_answer' => 'not-empty',
	  				'filter_customer_id' => $customer_id
	      		);
      			
      			$total_participant = $this->model_survey_survey->getTotalParticipent($question_data);
      			
      			$get_answer = "";
      			if($total_participant) {
      				$get_answer = $this->model_survey_survey->getAnswer($question_data);
      				$get_answer = $get_answer['answer'];
      			}
	      		
	      		$answer[] = array(
					'text' => $get_answer,
					'value' => $total_participant				
				);
      		break;
      		case 'checkbox':
      		case 'radio':
	      		$question_info['lable'] = str_replace("\r","", $question_info['lable']);
	      		$all_lables = explode("\n", $question_info['lable']);
	      		if(!empty($question_info['value'])) {
	      			$question_info['value'] = str_replace("\r","", $question_info['value']);
	      			$all_values = explode("\n", $question_info['value']);
	      		} else {
	      			$all_values = $all_lables;
	      		}      			
	      		
	      		if(!empty($check_answer['answer']))
	      			$default_value = $check_answer['answer'];
	      		else
	      			$default_value = $question_info['default'];
	      		
	      		foreach($all_lables as $key => $each_value) {
	      			      			
	      			$default = 0;
	      			if($default_value == $all_values[$key])
	      				$default = 1;
	      				
	      		    $options[] = array(
		      			'lable' => $all_lables[$key],
		      			'value' => $all_values[$key],
		      			'default' => $default
		      		);	
	      		}
	      		
	      		foreach($options as $each_option) {
	      			
	      			$question_data = array(
	      				'filter_survey_id' => $survey_id,
	      				'filter_question_id' => $question_id,
	      				'filter_customer_id' => $customer_id,      				
	      				'filter_answer' => $each_option['value']
	      			);
	      			
	      			$total_participant = $this->model_survey_survey->getTotalParticipent($question_data);
	
					$answer[] = array(
						'text' => $each_option['lable'],
						'value' => $total_participant,
						'link' => $this->url->link('survey/survey/participents', 'token=' . $this->session->data['token'] . '&survey_id=' . $survey_id . '&question_id=' . $question_id . '&filter_answer=' . $each_option['value'])
					);      			
	      		}
	      		
    			break;
    			case 'default' :
    			case 'lable' :
    			$answer = array();
    			break;
    		}
				
			$this->data['questions'][] = array(
				'question_id' => $result['question_id'],
				'title'          => $result['title'],
				'sort_order'     => $result['sort_order'],
				'answer'     => $answer,
				'selected'       => isset($this->request->post['selected']) && in_array($result['question_id'], $this->request->post['selected'])
			);
		}	
	
		$this->data['heading_title'] = $heading_title;

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_title'] = $this->language->get('column_title');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_answer_stats'] = $this->language->get('column_answer_stats');		
		$this->data['column_id'] = $this->language->get('column_id');		
		
		$this->data['button_back'] = $this->language->get('button_back');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_id'] = 'javascript:void(0)';
		$this->data['sort_title'] = 'javascript:void(0)';
		$this->data['sort_stats'] = 'javascript:void(0)';
		$this->data['sort_sort_order'] = 'javascript:void(0)';
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $survey_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('survey/survey', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'survey/answers.tpl';
		$this->children = array(
			'common/header',
			'common/footer',
		);
				
		$this->response->setOutput($this->render());
		
  	}   
	
   	private function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'survey/report')) {
			$this->error['warning'] = $this->language->get('error_permission');
    	}

		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}	
}
?>